package com.ibiaoke.boss.controller;

import com.ibiaoke.common.base.BaseController;
import com.ibiaoke.common.response.Res;
import com.ibiaoke.customer.entity.Customer;
import com.ibiaoke.order.entity.XcxAddress;
import com.ibiaoke.order.entity.vo.XcxAddressVo;
import com.ibiaoke.order.service.XcxAddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;

@Controller
public class XcxAddressController extends BaseController {

    @Autowired
    private XcxAddressService xcxAddressService;

    /**
     * 保存收货地址信息
     */
    @RequestMapping("saveXcxAddress")
    @ResponseBody
    public Res saveXcxAddress(@Validated XcxAddressVo xcxAddressVo, BindingResult result ) {
        handleValidate(result);
        Customer customer = getLoginUser();
        xcxAddressVo.setUserId(customer.getCustomerId());
        xcxAddressService.saveXcxAddress(xcxAddressVo);
        return new Res();
    }


    /**
     * 更改收货地址信息
     */
    @RequestMapping("/updateXcxAddress")
    @ResponseBody
    public Res updateOrderAddress(XcxAddressVo xcxAddressVo) {
        Customer customer = getLoginUser();
        xcxAddressVo.setUserId(customer.getCustomerId());
        xcxAddressService.updateXcxAddress(xcxAddressVo);
        return new Res();
    }

    /**
     * 查询用户所有地址，根据店铺距离排序
     *
     * @return
     */
    @RequestMapping("/listXcxAddressByShop")
    @ResponseBody
    public Res listXcxAddressByShop(Integer shopId) {
        Res res =new Res();
        Customer customer = getLoginUser();
        List<XcxAddressVo> xcxAddressVOList = new ArrayList<XcxAddressVo>();
        List<XcxAddress> xcxAddressList = xcxAddressService.listXcxAddress(customer.getCustomerId());
        for(XcxAddress xcxAddress : xcxAddressList){
            XcxAddressVo  xcxAddressVo = new XcxAddressVo();
            xcxAddressVo.setDistince(xcxAddressService.getDistinceByShopAndAddress(xcxAddress,shopId));
            xcxAddressVo.setStatus(xcxAddressVo.getDistince() >= 15 ? 0 : 1);
            BeanUtils.copyProperties(xcxAddress,xcxAddressVo);
            xcxAddressVOList.add(xcxAddressVo);
        }
        res.addResponse("xcxAddressList", xcxAddressVOList);
        return res;
    }

    /**
     * 查询用户所有地址
     *
     * @return
     */
    @RequestMapping("/listXcxAddress")
    @ResponseBody
    public Res listXcxAddress() {
        Res res =new Res();
        Customer customer = getLoginUser();
        List<XcxAddress> xcxAddressList = xcxAddressService.listXcxAddress(customer.getCustomerId());
        res.addResponse("xcxAddressList", xcxAddressList);
        return res;
    }

    /**
     * 获取单个地址信息
     * @param addressId
     * @return res
     */
    @RequestMapping("/getXcxAddress")
    @ResponseBody
    public Res getXcxAddress(Integer addressId) {
        Res res = new Res();
        XcxAddress xcxAddress = xcxAddressService.getXcxAddress(addressId);
        res.addResponse("orderAddress", xcxAddress);
        return res;
    }


    /**
     *删除收货地址
     * @param addressId
     * @return
     */
    @RequestMapping("/deleteXcxAddress")
    @ResponseBody
    public Res deleteXcxAddress(Integer addressId) {
        xcxAddressService.deleteOrderAddress(addressId);
        return Res.create();
    }

}
